package com.codeoss.framework.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codeoss.framework.response.AttachmentResponse;
import com.codeoss.framework.service.IAttachmentService;
import com.codeoss.framework.util.IOUtil;
import com.codeoss.module.ir.model.IrAttachment;
import com.codeoss.module.ir.service.IIrAttachmentService;

@Service
public class AttachmentService implements IAttachmentService {
	@Value("${framework.base-path}")
	private String basePath;
	 
    @Autowired
    private IIrAttachmentService irAttachmentService;
	@Override
	public AttachmentResponse init(String model,Long recordId) {
		// TODO Auto-generated method stub
		AttachmentResponse attachmentResponse=new AttachmentResponse();
		QueryWrapper<IrAttachment> query=new QueryWrapper<IrAttachment>();
		query.eq("model", model);
		query.eq("record_id", recordId);
		List<IrAttachment> attachmentList=irAttachmentService.list(query);
		attachmentResponse.setAttachmentList(attachmentList);
		return attachmentResponse;
	}
	
	@Override
	@Transactional
	public AttachmentResponse upload(String model, Long recordId, MultipartFile file) {
		// TODO Auto-generated method stub
		AttachmentResponse attachmentResponse=new AttachmentResponse();
		String originalFilename=file.getOriginalFilename();
		String ext=originalFilename.substring(originalFilename.lastIndexOf(".")+1);
		String filename=UUID.randomUUID().toString();
		filename=filename+"."+ext;
		File newFile=new File(basePath+"/attachment"+File.separator+filename);
		FileOutputStream fos=null;
		InputStream is=null;
		try {
			fos=new FileOutputStream(newFile);
			is=file.getInputStream();
			byte b[]=new byte[1024];
			int len;
			while((len=is.read(b))!=-1) {
				fos.write(b,0,len);
			}
			
			IrAttachment irAttachment=new IrAttachment();
			irAttachment.setModel(model);
			irAttachment.setRecordId(recordId);
			irAttachment.setOriginalFilename(originalFilename);
			irAttachment.setFilename(filename);
			irAttachmentService.insert(irAttachment);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}finally {
			IOUtil.closeOutputStream(fos);
			IOUtil.closeInputStream(is);
		}
		
		return attachmentResponse;
	}

	@Override
	public AttachmentResponse download(Long id)throws IOException {
		// TODO Auto-generated method stub
		AttachmentResponse attachmentResponse=new AttachmentResponse();
		IrAttachment irAttachment=irAttachmentService.getById(id);
		attachmentResponse.setOriginalFilename(irAttachment.getOriginalFilename());
		String filename=basePath+"/attachment"+File.separator+irAttachment.getFilename();
		File file=new File(filename);
		FileInputStream fis=new FileInputStream(file);
		attachmentResponse.setInputStream(fis);
		return attachmentResponse;
	}

	@Override
	@Transactional
	public AttachmentResponse deleteAttachment(Long id) {
		// TODO Auto-generated method stub
		AttachmentResponse attachmentResponse=new AttachmentResponse();
		IrAttachment irAttachment=irAttachmentService.getById(id);
		irAttachmentService.delete(irAttachment);
		String filename=basePath+"/attachment"+File.separator+irAttachment.getFilename();
		File file=new File(filename);
		file.delete();
		return attachmentResponse;
	}
}
